import org.junit.jupiter.api.Test;

public class _06_Z字形变换 {


    @Test
    void test(){

        System.out.println(convert("PAYPALISHIRING", 3));
    }

    public String convert(String s, int numRows) {
        if (s.length()==0||s==null||numRows<=1) return s;
        StringBuilder[] array=new StringBuilder[numRows];
        for (int i = 0; i <numRows; i++) {
            array[i]=new StringBuilder();
        }
        //当前的索引
        int index=0;
        //每次增加一
        int dir=1;
        char[] chars = s.toCharArray();
        for (char c : chars) {
            array[index].append(c);
            index+=dir;
            if (index==0||index==numRows-1){
                dir=-dir;
            }
        }
        StringBuilder result=new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            result.append(array[i]);
        }
        return result.toString();
    }
}
